<template>
  <z-modal :visible="visible" title="自定义列" @onConfirm="onConfirm" class="z-filter-table">
    <ZCheckBoxGroup
      :groups="columns"
      :default-values="defaultValue"
      @change="onChange"
    ></ZCheckBoxGroup>
  </z-modal>
</template>
<script>
import ZModal from "@/components/ZBase/ZModal";
import ZCheckBoxGroup from "@/components/ZBase/ZCheckBox/ZCheckBoxGroup";
export default {
  name: "detailModel",
  components: {
    ZModal,
    ZCheckBoxGroup,
  },

  props: {
    origninColumns: {
      type: Array,
      default: () => [],
    },
  },
  //打开弹窗就调用
  onCreateApi() {
    this.visible = true;
    this.columns = this.origninColumns?.map((item) => {
      return {
        label: item.title,
        value: item.title,
        hidden: item.hidden,
      };
    });
  },
  //关闭弹窗就调用
  unCreateApi() {
    this.visible = false;
  },
  data() {
    return {
      visible: false,
      columns: [],
      values: [],
    };
  },
  computed: {
    defaultValue({ columns }) {
      const values = columns?.filter((item) => !item.hidden).map((item) => item.value);
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      this.values = values;
      return values;
    },
  },
  methods: {
    onChange(v) {
      this.values = v;
    },
    onConfirm() {
      if (JSON.stringify(this.values) != JSON.stringify(this.defaultValue)) {
        this.origninColumns.forEach((item) => {
          const findIndex = this.values.findIndex((v) => v === item.title);
          if (findIndex === -1) {
            this.$set(item, "hidden", true);
          } else {
            this.$set(item, "hidden", false);
          }
        });
      }
      this.visible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.z-filter-table {
  ::v-deep .el-checkbox {
    width: 140px;
    margin-right: 0;
    margin-bottom: 20px;
  }
}
</style>
